package com.example.chinesenation.Config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author sakuraFallingDown
 * @version 1.0
 * @date 2020/12/8 23:03
 */
@Configuration
public class DruidConfig {

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource getDataSource(){
        return new DruidDataSource();
    }
    @Bean
    public ServletRegistrationBean servletRegistrationBean(){
        ServletRegistrationBean sb=new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
        Map<String ,String> m=new HashMap<>();
        m.put("loginUsername","root");
        m.put("loginPassword","yuan");
        m.put("resetEnable", "false");
        sb.setInitParameters(m);
        return sb;
    }
    @Bean
    public FilterRegistrationBean filterRegistrationBean(){
        FilterRegistrationBean frb=new FilterRegistrationBean(new WebStatFilter());
        frb.addUrlPatterns("/*");
        Map<String ,String> m=new HashMap<>();
        m.put("exclusions","*.js, *.jpg, *.png, *.css, /druid/*");
        frb.setInitParameters(m);
        return frb;
    }

}
